using NUnit.Framework;

namespace Rsdn.SmartApp
{
	[TestFixture]
	public class ServicesHelperTest
	{
		private ServiceManager _serviceManager;

		[SetUp]
		protected void SetUp()
		{
			_serviceManager = new ServiceManager();
		}

		[Test]
		public void PublishInstance()
		{
			_serviceManager.Publish(typeof(ISampleService), new SampleService());
			Assert.IsNotNull(_serviceManager.GetService<ISampleService>());
		}

		[Test]
		public void PublishCreator()
		{
			_serviceManager.Publish(typeof(ISampleService), () => new SampleService());
			Assert.IsNotNull(_serviceManager.GetService<ISampleService>());
		}

		[Test]
		public void GetService()
		{
			_serviceManager.Publish<ISampleService>(new SampleService());
			Assert.IsNotNull(_serviceManager.GetService(typeof(ISampleService)));
		}

		[Test]
		public void AssignServices()
		{
			var ss = new SampleService();
			_serviceManager.Publish<ISampleService>(ss);
			var su = new ServiceUser();
			su.AssignServices(_serviceManager);

			Assert.AreEqual(ss, su.Svc1);
			Assert.AreEqual(ss, su.Svc2);
			Assert.AreEqual(ss, su.Svc3);
		}

		[Test]
		[ExpectedException(typeof(ServiceNotFoundException))]
		public void AssignNonExistentRequiredService()
		{
			var su = new ServiceUser();
			su.AssignServices(_serviceManager);
		}

		private static void Sample(IServiceProvider provider, object instance)
		{
			var su = (ServiceUser)instance;

			// Not required
			su._svc3 = provider.GetService<ISampleService>();

			// Required
			su._svc3 = provider.GetRequiredService<ISampleService>();
		}
	}
}
